import CustomMaterial from "./CustomMaterial";

//GameShaderObj
export default class GameShaderObj extends Laya.EventDispatcher {

    private static init_: boolean = true;

    constructor() {
        super();
        if (GameShaderObj.init_) {
            this.initShader();
            GameShaderObj.init_ = true;
        }
    }

    private s1: Laya.BaseMaterial[];
    private s2: Laya.BaseMaterial[];
    private ms: Laya.MeshSprite3D;

    public static sp: Laya.Sprite3D;

    

    private initShader(): void {

        var attributeMap: Object = {
            'a_Position': Laya.VertexMesh.MESH_POSITION0,
            'a_Normal': Laya.VertexMesh.MESH_NORMAL0,
            'a_Texcood0': Laya.VertexMesh.MESH_TEXTURECOORDINATE0
        };
        var uniformMap: Object = {
            'u_MvpMatrix': Laya.Shader3D.PERIOD_SPRITE,
            'u_WorldMat': Laya.Shader3D.PERIOD_SPRITE,
            'u_MainTex': Laya.Shader3D.PERIOD_MATERIAL,
            'u_MainCol': Laya.Shader3D.PERIOD_MATERIAL,
            'u_marginalColor': Laya.Shader3D.PERIOD_MATERIAL
        };
        var vs: string = `
        #include "Lighting.glsl"; 
        attribute vec4 a_Position;
        attribute vec3 a_Normal;
        attribute vec2 a_Texcood0;

        uniform mat4 u_MvpMatrix;
        uniform mat4 u_WorldMat;

        varying vec3 v_Normal;
        varying vec2 uv;
        varying vec4 worldPos;

        void main()
        {
            gl_Position = u_MvpMatrix * a_Position;
            mat3 worldMat = mat3(u_WorldMat);
            v_Normal = worldMat * a_Normal;
            worldPos = u_WorldMat * a_Position;
            uv = a_Texcood0;
            gl_Position = remapGLPositionZ(gl_Position); 
        }`;
        var ps: string = `
        #ifdef FSHIGHPRECISION
        precision highp float;
        #else
        precision mediump float;
        #endif

        uniform sampler2D u_MainTex;
        uniform vec3 u_MainCol;

        uniform vec3 u_marginalColor;

        varying vec3 v_Normal;
        varying vec2 uv;
        varying vec4 worldPos;

        void main()
        {
            vec4 col = texture2D(u_MainTex, uv);
            //gl_FragColor = vec4(worldPos.xyz, 1.0);
            float d = distance(worldPos.xyz, vec3(.0, .0, .0)) * 0.15;
            col.xyz *= d;
            col.xyz *= u_MainCol;
            col.a = 0.5;
            vec3 Emissive =  15.0 * u_marginalColor; 
            gl_FragColor = col + vec4(Emissive,0.05);
        }`;
        //创建自定义Shader
        var customShader: Laya.Shader3D = Laya.Shader3D.add("CustomShader");
        //为Shader添加SubShader
        var subShader: Laya.SubShader = new Laya.SubShader(attributeMap, uniformMap);
        customShader.addSubShader(subShader);
        //为SubShader添加ShaderPass
        // subShader.addShaderPass(vs, ps);

        let stateMap = {
            's_Cull': Laya.Shader3D.RENDER_STATE_CULL,
            's_Blend': Laya.Shader3D.RENDER_STATE_BLEND,
            's_BlendSrc': Laya.Shader3D.RENDER_STATE_BLEND_SRC,
            's_BlendDst': Laya.Shader3D.RENDER_STATE_BLEND_DST,
            's_DepthTest': Laya.Shader3D.RENDER_STATE_DEPTH_TEST,
            's_DepthWrite': Laya.Shader3D.RENDER_STATE_DEPTH_WRITE
        };
         subShader.addShaderPass(vs, ps, stateMap);

    }
}